package com.lzit.smart_canteen.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.lzit.smart_canteen.domain.CartItem;
import com.lzit.smart_canteen.domain.Food;
import com.lzit.smart_canteen.domain.OrderItem;
import com.lzit.smart_canteen.service.FoodService;
import com.lzit.smart_canteen.service.ItemService;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class FoodController {
    @Autowired
    private FoodService foodService;
    @Autowired
    private ItemService itemService;
    //展示窗口食物(客户)
    @GetMapping("/toWindow")
    public String toWindow(Model model, HttpServletRequest request){
        Integer window = Integer.valueOf(request.getParameter("w"));
        List<Food> foods = foodService.queryFoodsByWindow(window);
        model.addAttribute("foods", foods);
        return "/mobile/window";
    }
    //展示窗口食物(员工)
    @GetMapping("/toWindow_staff")
    public String toWindow_staff(HttpServletRequest request, Model model){
        Integer window = Integer.valueOf(request.getParameter("w"));
        List<Food> foods = foodService.queryFoodsByWindow(window);
        model.addAttribute("foods", foods);
        return "/manage/window";
    }
    //展示做餐窗口
    @GetMapping("/toCookWindow")
    public String toCookWindow(HttpServletRequest request, Model model) {
        Integer window = Integer.valueOf(request.getParameter("w"));
        List<OrderItem> orderItems = itemService.findOrderItemsByWindowAndStatus(window,"process");
        if (orderItems.isEmpty()) {
            model.addAttribute("items",null);
        } else {
            model.addAttribute("items",orderItems);
        }
        return "/manage/cookWindow";
    }
    //展示窗口菜单状态
    @GetMapping("/toMenuStatus")
    public String toMenuStatus(HttpServletRequest request, Model model){
        Integer window = Integer.valueOf(request.getParameter("w"));
        List<Food> foods = foodService.queryFoodsByWindow(window);
        model.addAttribute("foods", foods);
        return "/manage/menuStatus";
    }
    //前往新增菜单页面
    @GetMapping("/toAddMenu")
    public String toAddMenu(){
        return "/manage/addMenu";
    }
    //新增菜单
    @PostMapping("/addMenuResult")
    public String addMenuResult (HttpServletRequest request){
        Food food = new Food();
        String name = request.getParameter("name");
        BigDecimal price = new BigDecimal(request.getParameter("price"));
        Integer window = Integer.valueOf(request.getParameter("window"));
        String status = request.getParameter("status");
        food.setName(name);
        food.setPrice(price);
        food.setWindow(window);
        food.setStatus(status);
        foodService.saveFood(food);
        return "/manage/addMenuSuccess";
    }
    //更改菜单状态
    @GetMapping("/alertStatus")
    public void alertStatus(HttpServletRequest request,HttpServletResponse response){
        Integer fid = Integer.valueOf(request.getParameter("fid"));
        String status = request.getParameter("status");
        foodService.updateStatusById(fid,status);

    }

}
